#ifndef		QSTRINGBUFFER_H
#define		QSTRINGBUFFER_H

#include <windows.h>

#ifdef UNICODE  

#define QStringToTCHAR(x)     (wchar_t*) x.utf16()  

#define PQStringToTCHAR(x)    (wchar_t*) x->utf16()  

#define TCHARToQString(x)     QString::fromUtf16(reinterpret_cast<const ushort *>(x))  

#define TCHARToQStringN(x,y)  QString::fromUtf16(reinterpret_cast<const ushort *>(x),(y))  

#else  

#define QStringToTCHAR(x)     x.local8Bit().constData()  

#define PQStringToTCHAR(x)    x->local8Bit().constData()  

#define TCHARToQString(x)     QString::fromLocal8Bit((x))  

#define TCHARToQStringN(x,y)  QString::fromLocal8Bit((x),(y))  

#endif  

#define Q_WriteBuf_TYPE(pBuffer, type, value, size)\
{(*(type*)pBuffer) = value; size += sizeof(type);pBuffer += sizeof(type);}


#define Q_ReadBuf_TYPE(pBuffer, type, value, size)\
{value = (*(type*)pBuffer);size += sizeof(type);pBuffer += sizeof(type);}

#define Q_OffsetBufPtr_TYPE(pBuffer, type, value, size)\
{value = (type*)pBuffer;size += sizeof(type);pBuffer += sizeof(type);}

#define Q_WriteStr_Size(length)   (sizeof(DWORD) +  (length + 1)* sizeof(TCHAR))

#define Q_WriteStr_TYPE(pBuffer, length, value, size)\
{ *((DWORD *)pBuffer)   = length; pBuffer += sizeof(DWORD); size += sizeof(DWORD);\
	if(length>0) lstrcpyn((TCHAR *)pBuffer,QStringToTCHAR(value),length+1);\
	*((TCHAR *)(pBuffer + length * sizeof(TCHAR))) = 0;\
	pBuffer += (length + 1)* sizeof(TCHAR);size += (length + 1)* sizeof(TCHAR);}


#define Q_ReadStr_TYPE(pBuffer, value, size)\
{DWORD sLength = *((DWORD *)pBuffer);   pBuffer += sizeof(DWORD);	 size += sizeof(DWORD);\
	value = TCHARToQString((TCHAR *)pBuffer);value = value.left(sLength);\
	pBuffer += (sLength + 1)* sizeof(TCHAR);size += (sLength + 1)* sizeof(TCHAR);}


#define Q_MovePosition(dwOffset, pBuffer, dwSize) {pBuffer += dwOffset; dwSize+= dwOffset;}
#define Q_BufPtr_TYPE(pBuffer, type, value) {value = (type*)pBuffer;}

#define Q_WriteBuffer_TYPE(pBuffer, count, value, size)\
{*((DWORD *)pBuffer) = count; pBuffer += sizeof(DWORD); size += sizeof(DWORD);\
	memcpy_s(pBuffer,count,value,count);size += count;pBuffer += count;}

#define Q_ReadBuffer_TYPE(pBuffer, count, value, size)\
{DWORD sLength = *((DWORD *)pBuffer); pBuffer += sizeof(DWORD); size += sizeof(DWORD);\
	memcpy_s(value,count,pBuffer,sLength);size += sLength;pBuffer += sLength;}



#endif